package com.kaka.controller;

import com.kaka.anno.Log;
import com.kaka.pojo.Dept;
import com.kaka.pojo.Result;
import com.kaka.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    //查询所有部门
    @GetMapping
    public Result selectAll() {
        log.info("查询全部部门数据");
        List<Dept> deptList = deptService.selectAll();
        return Result.success(deptList);
    }

    //根据id删除部门
    @Log
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("根据id删除部门:{}", id);
        deptService.deleteById(id);
        return Result.success();
    }

    //添加部门
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept) {
        log.info("新增部门:{}", dept);
        deptService.add(dept);
        return Result.success();
    }

    //根据id查询部门
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        log.info("查找部门:{}", id);
        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }

    //修改部门
    @Log
    @PutMapping
    public Result change(@RequestBody Dept dept) {
        log.info("修改部门:{}", dept);
        deptService.change(dept);
        return Result.success();
    }
}
